package com.shitou.shitoumember.controller;


import com.shitou.shitoucommons.annotation.CheckUserLogin;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoumember.entity.MemberCollectSpuEntity;
import com.shitou.shitoumember.entity.MemberEntity;
import com.shitou.shitoumember.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.shitou.shitoumember.service.MemberService;
import com.shitou.shitoucommons.utils.RespBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author shitouren
 * @email 3076767823@qq.com
 * @since  2023-03-28 16:52:06
 */
@Api(value = "会员接口")
@RestController
@RequestMapping("shitoumember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return RespBean.success().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public RespBean info(@PathVariable("attrId") Long attrId){
        MemberEntity attr = memberService.getById(attrId);

        return RespBean.success().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody MemberEntity attr){
        memberService.save(attr);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody MemberEntity attr){
        memberService.updateById(attr);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] attrIds){
        memberService.removeByIds(Arrays.asList(attrIds));

        return RespBean.success();
    }

    /**
     * 登录
     * @param loginVo 登录映射实体vo
     * @param bindingResult 表示绑定结果的通用接口
     * @return RespBean
     */
    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public RespBean doLogin(@Validated @RequestBody LoginVo loginVo, HttpServletRequest request,
                            HttpServletResponse response, BindingResult bindingResult) {
        System.out.println(loginVo);
        return memberService.doLogin(loginVo, request, response, false);
    }


    /**
     * 注册
     * @param registerVo 注册实体映射vo
     * @see RegisterVo
     * @param bindingResult 表示绑定结果的通用接口
     * @return RespBean
     */
    @ApiOperation(value = "注册接口")
    @PostMapping("/register")
    public RespBean doRegister(@Validated @RequestBody RegisterVo registerVo, BindingResult bindingResult) {
        return memberService.doRegister(registerVo, false);
    }


    /**
     * 登出
     * @param memberId 会员id
     * @return RespBean
     */
    @ApiOperation(value = "用户登出接口，需要登陆校验")
    @CheckUserLogin
    @PostMapping("/loginOut/{memberId}")
    public RespBean doLoginOut(@PathVariable long memberId) {
        if (memberId == 0 || memberId < 0)
            throw new RuntimeException("登出异常");
        return memberService.doLoginOut(memberId);
    }


    /**
     * 获取前台需要的会员的所有信息
     * @see MemberInfoVo
     * @return UpdatePasswordVo
     */
    @ApiOperation(value = "获取前台需要的会员的所有信息,需要登录校验")
    @CheckUserLogin
    @PostMapping("/memberInfo")
    public RespBean memberInfo(HttpServletRequest request, HttpServletResponse response){
        return memberService.memberInfo(request, response);
    }


    /**
     * 更新头像
     * @param headerVo 映射类
     * @see UpdateHeaderVo
     * @return UpdatePasswordVo
     */
    @ApiOperation(value = "用户更新头像时调用，需要登录校验")
    @CheckUserLogin
    @PostMapping("/updateHeader")
    public RespBean updateHeader(@Validated @RequestBody UpdateHeaderVo headerVo){
        return memberService.updateHeader(headerVo);
    }


    /**
     * 更新nickname和gender
     * @param vo 映射类
     * @see UpdateNicknameGenderVo
     * @return UpdatePasswordVo
     */
    @ApiOperation(value = "更新会员的nickname和gender，需要登录校验")
    @CheckUserLogin
    @PostMapping("/updateInfo")
    public RespBean updateNicknameGender(@Validated @RequestBody UpdateNicknameGenderVo vo){
        return memberService.updateNicknameGender(vo);
    }


    /**
     * 更新邮箱
     * @param emailVo 映射类
     * @see UpdateEmailVo
     * @return RespBean
     */
    @ApiOperation(value = "更新用户的邮箱，需要登录校验")
    @CheckUserLogin
    @PostMapping("/updateEmail")
    public RespBean updateEmail(@RequestBody UpdateEmailVo emailVo){
        return memberService.updateEmail(emailVo);
    }


    /**
     * 找回密码
     * @param passwordVo 映射类
     * @see UpdatePasswordVo
     * @return RespBean
     */
    @ApiOperation(value = "用户忘记密码时，可以通过该接口通过邮箱进行密码的找回")
    @PostMapping("/updatePassword")
    public RespBean updatePassword(@RequestBody UpdatePasswordVo passwordVo){
        return memberService.findPassword(passwordVo);
    }


    /**
     * 远程调用获取用户信息
     * @param memberId 会员用户id
     * @return RespBean
     */
    @ApiOperation(value = "远程调用接口，获取用户的信息，用以其它操作")
    @CheckUserLogin(value = false)
    @PostMapping("/getMemberVo")
    public RespBean getMemberVo(@RequestParam long memberId, HttpServletRequest request, HttpServletResponse response){
        return memberService.getMemberVo(memberId, request, response);
    }


    /**
     * 根据用户ids获取用户信息
     * @param memberIds 多个用户的id
     * @return RespBean
     */
    @ApiOperation(value = "根据多个会员的id获取用户的信息")
    @CheckUserLogin
    @PostMapping("/getMemberInfoByMemberIds")
    public RespBean getMemberInfoByMemberIds(@RequestParam long[] memberIds){
        return memberService.getMemberInfoByMemberIds(memberIds);
    }


    /**
     * 远程调用，校验是否登录
     * @param token
     * @return
     */
    @ApiOperation(value = "远程调用，简单的登录校验接口，其它服务调用该接口判断该会员是否登录")
    @CheckUserLogin(value = false)
    @PostMapping("/checkLogin")
    public RespBean checkLogin(@RequestParam String token, HttpServletResponse response){
        return memberService.checkLogin(token, response);
    }


    /**
     * 前端调用，校验该页面是否要需要登陆才能访问
     * @param request
     * @return
     */
    @Deprecated
    @ApiOperation(value = "该方法已经抛弃")
    @CheckUserLogin(value = false)
    @PostMapping("/foreEndCheckLogin")
    public RespBean fore_endCheckLogin(HttpServletRequest request, HttpServletResponse response){
        return memberService.fore_endCheckLogin(request, response);
    }


    /**
     * 获取商品发布者的信息
     * @param memberId
     * @return
     */
    @ApiOperation(value = "通过会员的id，获取用户的信息，主要用于获取商品发布者的信息")
    @PostMapping("/getPublisher")
    public RespBean getPublisher(@RequestParam Long memberId){
        return memberService.getPublisher(memberId);
    }

}
